package com.calendar.event;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import com.calendar.add.AddView;
import com.calendar.backend.Event;
import com.calendar.backend.Events;
import com.calendar.backend.Help;
import com.calendar.day.DayView;
import com.calendar.month.CalendarView;
import com.calendar.widget.R;

import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class EventView extends Activity implements OnGesturePerformedListener {
	/** Called when the activity is first created. */

	int dayValue;
	int monthValue;
	int yearValue;
	CharSequence name;
	long startTime;
	long length;
	String eventID;

	public static final int MENU_MONTH = Menu.FIRST + 1;
	public static final int MENU_DAY = Menu.FIRST + 2;
	public static final int MENU_EDIT = Menu.FIRST + 3;
	public static final int MENU_SETTINGS = Menu.FIRST + 4;
	public static final int MENU_EXIT = Menu.FIRST + 5;
	public static final int MENU_HELP = Menu.FIRST + 6;


	TextView title;
	TextView subject;
	TextView time;
	TextView location;
	TextView alarms;
	TextView attendees;
	TextView description;


	GestureLibrary mLibrary;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.event_view);


		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			dayValue = extras.getInt("day");
			yearValue = extras.getInt("year");
			eventID = extras.getString("eventID");
		}

		title = (TextView) findViewById(R.id.title);
		subject = (TextView) findViewById(R.id.subject);
		time = (TextView) findViewById(R.id.time);
		location = (TextView) findViewById(R.id.location);
		alarms = (TextView) findViewById(R.id.alarms);
		attendees = (TextView) findViewById(R.id.attendees);
		description = (TextView) findViewById(R.id.description);

		Calendar cal = Calendar.getInstance();
		cal.set(yearValue, monthValue, dayValue, 0, 0, 0);
		Events eventFinder = new Events();
		//new Event(true, getBaseContext(),"",1 + "", "Test Event", "bah", "location", cal.getTimeInMillis(), cal.getTimeInMillis() + 3600000, false, false);
		Vector<com.calendar.backend.Event> events = eventFinder.findEvents(getBaseContext(), cal.getTimeInMillis(), cal.getTimeInMillis() + 86400000);
		for(Event e : events){
			if(e.getEventID() == eventID){
				title.setText("Title: " + e.getEventTitle());
				time.setText("Start Time: " + e.getStartTime());
				description.setText("Description:" + e.getDescription());
				break;
			}
		}

		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(this);

		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()) {
			finish();
		}
	}

	public void onBackPressed(){
		Intent myIntent = new Intent(getBaseContext(), DayView.class);
		myIntent.putExtra("day", dayValue);
		myIntent.putExtra("month", monthValue);
		myIntent.putExtra("year", yearValue);
		startActivityForResult(myIntent, 0);
		this.finish();
	}


	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

		// We want at least one prediction
		if (predictions.size() > 0 && predictions.get(0).score > 1.0
				&& gesture.getStrokesCount() > 1) {
			String what = "";
			for (int i = 0; i < predictions.size(); i++) {
				if (predictions.get(i).name.equals("Add1")
						|| predictions.get(i).name.equals("Add2")
						|| predictions.get(i).name.equals("Add3")
						|| predictions.get(i).name.equals("Add4")
						|| predictions.get(i).name.equals("Add5")
						|| predictions.get(i).name.equals("Add6")
						|| predictions.get(i).name.equals("Add7")
						|| predictions.get(i).name.equals("Add8")
						|| predictions.get(i).name.equals("Agenda")) {
					what = predictions.get(i).name;
					break;
				}
			}
			if(what.equals("Agenda")){
				Intent myIntent = new Intent(getBaseContext(), DayView.class);
				startActivityForResult(myIntent, 0);
			}else if(what.equals("Add1") || what.equals("Add2")
					|| what.equals("Add3") || what.equals("Add4")
					|| what.equals("Add5") || what.equals("Add6")
					|| what.equals("Add7") || what.equals("Add8")){
				Intent myIntent = new Intent(getBaseContext(), AddView.class);
				startActivityForResult(myIntent, 0);
				this.finish();
			}
			Toast.makeText(this, what, Toast.LENGTH_SHORT).show();
		} else if (predictions.size() > 0 && predictions.get(0).score > 1.0
				&& gesture.getStrokesCount() == 1) {

			String what = "";
			for (int i = 0; i < predictions.size(); i++) {
				if (predictions.get(i).name.equals("Next")
						|| predictions.get(i).name.equals("Previous")
						|| predictions.get(i).name.equals("Help")
						|| predictions.get(i).name.equals("Edit")
						|| predictions.get(i).name.equals("Edit2")
						|| predictions.get(i).name.equals("Month")
						|| predictions.get(i).name.equals("Month2")) {
					what = predictions.get(i).name;
					break;
				}
			}
			Toast.makeText(this, what, Toast.LENGTH_SHORT).show();
			if(what.equals("Month") || what.equals("Month2")){
				Intent myIntent = new Intent(getBaseContext(), CalendarView.class);
				startActivityForResult(myIntent, 0);
				this.finish();
			}else if(what.equals("Edit") || what.equals("Edit2")){
				Intent myIntent = new Intent(getBaseContext(), AddView.class);
				myIntent.putExtra("day", dayValue);
				myIntent.putExtra("month", monthValue);
				myIntent.putExtra("year", yearValue);
				myIntent.putExtra("eventID", eventID);
				startActivityForResult(myIntent, 0);
				this.finish();
			}else if(what.equals("Help")){
				Intent myIntent = new Intent(getBaseContext(), Help.class);
				startActivityForResult(myIntent, 0);
			}
		}
	}

	// Goes to the dayView from this view
	public void newActivity(int key) {
		Intent myIntent = new Intent(getBaseContext(), DayView.class);
		myIntent.putExtra("day", key);
		myIntent.putExtra("month", monthValue);
		myIntent.putExtra("year", yearValue);
		myIntent.putExtra("eventID", eventID);
		startActivityForResult(myIntent, 0);
		this.finish();
	}

	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_MONTH, 0, "Month").setIcon(
				android.R.drawable.ic_menu_month);
		menu.add(0, MENU_DAY, 0, "Day").setIcon(
				android.R.drawable.ic_menu_day);
		menu.add(0, MENU_EDIT, 0, "Edit").setIcon(
				android.R.drawable.ic_menu_edit);
		// menu.add(0, MENU_SETTINGS, 0, "Settings").setIcon(
		// android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_SETTINGS, 0, "Settings").setIcon(
				android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_HELP, 0, "Help").setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(0, MENU_EXIT, 0, "Exit").setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent myIntent;
		switch (item.getItemId()) {
		case MENU_MONTH:
			myIntent = new Intent(getBaseContext(), CalendarView.class);
			startActivityForResult(myIntent, 0);
			this.finish();
			return true;
		case MENU_DAY:
			newActivity(dayValue);
			return true;
		case MENU_EDIT:
			myIntent = new Intent(getBaseContext(), AddView.class);
			myIntent.putExtra("day", dayValue);
			myIntent.putExtra("month", monthValue);
			myIntent.putExtra("year", yearValue);
			myIntent.putExtra("eventID", eventID);
			startActivityForResult(myIntent, 0);				
			this.finish();
			return true;
		case MENU_SETTINGS:

			return true;
		case MENU_HELP:
			myIntent = new Intent(getBaseContext(), Help.class);
			startActivityForResult(myIntent, 0);
			this.finish();
			return true;

		case MENU_EXIT:
			this.finish();
			return true;
		}
		return false;
	}
}